const { WebContentsView, BrowserWindow, ipcMain } = require('electron');
const path = require('node:path');

// async function createPdf(url) {
//     let view = new BrowserWindow()
//     console.log(view.webContents);

//     // await view.webContents.loadURL(url)
//     view.webContents.on('did-finish-load', () => {
//         view.webContents.print({
//             silent: false,
//             printBackground: true
//         }, (success, failureReason) => {
//             if (!success) {
//                 console.error(failureReason);
//             }
//         });
//     });
//     await view.webContents.loadFile('src/renderAPI/22.pdf')
//     // await view.webContents.print({
//     //     silent: true
//     // }, async (success, errorType) => {
//     //     await view.webContents.close();
//     //     view=null;
//     // })

// }


async function createPdf(data,eventName) {
    let win = new BrowserWindow({
        webPreferences: {
            preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY,
        },
        show: false
    })
    win.webContents.loadFile('src/renderAPI/ss.html')
    win.webContents.on('ready-to-show', () => {
        win.webContents.send('on-receive', eventName, data)
    })
    ipcMain.once(eventName, () => {
        console.log('pdf-print');
        
        win.webContents.print({
            silent: true,
        }, (success, failureReason) => {
            if (!success) {
                console.error(failureReason);
            }
        });
    })
}

module.exports = {
    createPdf,
}